package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.sgb.kjwl.view.ui.adapter.MultiDictAdapter4;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;

import java.util.ArrayList;
import java.util.List;

//多选下拉框样式二
public class MultiWindow4 extends BasePopWindow {

    private RecyclerView recyclerView;
    private TextView resetView;
    private TextView okView;
    private MultiDictAdapter4 dictAdapter;
    private List<DictEntity3> dictEntityList;
    private List<DictEntity3> checkDict3List;
    private SparseBooleanArray checkBooleanArray;
    private OnCallBack onCallBack;
    private OnCloseBack onCloseBack;
    private List<String> storeIds;

    public MultiWindow4(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_multi4;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        recyclerView = holder.getPop().findViewById(R.id.rlv_list);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        okView = holder.getPop().findViewById(R.id.ok_view);
        bgView = holder.getPop().findViewById(R.id.cover_layout);
        bgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                storeIds.clear();
                dictAdapter.resetCheckDatas();
            }
        });
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (onCallBack != null) {
                    onCallBack.result(storeIds, checkDict3List, checkBooleanArray);
                }
            }
        });
        storeIds = new ArrayList<>();
        dictAdapter = new MultiDictAdapter4(mContext, null);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(dictAdapter);
        dictAdapter.setMultiSelect(true);
        dictAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<DictEntity3>() {
            @Override
            public void onItemMultiClick(List<DictEntity3> dictEntity3s, int position) {
                checkDict3List = dictEntity3s;
                checkBooleanArray = dictAdapter.getCheckList();
                storeIds = getStoreIds(dictEntity3s);
            }
        });
    }

    public void setData(List<DictEntity3> dictEntityList) {
        this.dictEntityList = dictEntityList;
        dictAdapter.setDatas(dictEntityList);
    }

    public void setAreaCheckDefault(List<DictEntity3> data, SparseBooleanArray checkBooleanArray) {
        dictAdapter.setCheckDefultDatas(data, checkBooleanArray);
    }

    private List<String> getStoreIds(List<DictEntity3> dictEntity3s) {
        List<String> list = new ArrayList<>();
        for (DictEntity3 dictEntity3 : dictEntity3s) {
            list.add(dictEntity3.getId());
        }
        return list;
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (onCloseBack != null) {
            onCloseBack.dismiss();
        }
    }

    public interface OnCallBack {
        void result(List<String> storeIds, List<DictEntity3> checkDict3List, SparseBooleanArray checkBooleanArray);
    }

    public void setOnCallBack(OnCallBack onCallBack) {
        this.onCallBack = onCallBack;
    }

    public interface OnCloseBack {
        void dismiss();
    }

    public void setOnCloseBack(OnCloseBack onCloseBack) {
        this.onCloseBack = onCloseBack;
    }
}
